﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Cstudy.DAL
{
    public class users
    {
        /// <summary>
        /// 创建新用户
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <returns>返回成功与否</returns>
        public bool AddUser(MODEL.users user)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@username",user.UserName),
                new SqlParameter("@pwd",user.Pwd),
                new SqlParameter("@email",user.Email)
            };
            return COM.SQLHelper.ExecuteNonQuery("dbo.sp_AddUsers", parms);
        }

        public bool ExistUserName(string userName)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@username",userName)
            };
            if ((int)COM.SQLHelper.ExecuteScalar("sp_ExitsUserName", parms) == 0)
                return false;
            else
                return true;
        }

        public bool ExistEmail(string email)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@email",email)
            };
            if ((int)COM.SQLHelper.ExecuteScalar("sp_ExitsEmail", parms) == 0)
                return false;
            else
                return true;
        }

        public bool UpdateUser(MODEL.users user)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@userid",user.UserID),
                new SqlParameter("@pwd",user.Pwd),
                new SqlParameter("@email",user.Email),
                new SqlParameter("@userpower",user.UserPower),
                new SqlParameter("@sex",(int)user.Sex),
                new SqlParameter("@nickname",user.NickName),
                new SqlParameter("@telephone",user.Telephone),
                new SqlParameter("@birth",user.Birth),
                new SqlParameter("@headimage",user.Headimage),
                new SqlParameter("@info",user.Info)
            };
            return COM.SQLHelper.ExecuteNonQuery("sp_UpdateUser", parms);
        }

        public DataSet UserLogin(MODEL.users user)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@username",user.UserName),
                new SqlParameter("@userpwd",user.Pwd)
            };
            return COM.SQLHelper.ExecuteQuery("sp_UserLogin", parms);
        }

        public DataSet SelectUserByID(int id)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@id",id)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectUserByID", parms);
        }

        public DataSet SelectUserByName(string name)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@username",name)
            };
            return COM.SQLHelper.ExecuteQuery("sp_SelectUserByName", parms);
        }

        public DataSet SelectUser(int currentPage, int pagesize, out int num, string returnstring)
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter("@count",SqlDbType.Int)
            };
            parms[0].Direction = ParameterDirection.Output;
            return COM.SQLTools.ExecuteQuery("sp_SelectUser", parms, currentPage, pagesize, out num, returnstring);
        }
    }
}
